﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using SW.Winform.Commons;
using System.Threading;

namespace SW.ImgProcess.WinForm
{
    public delegate void UpdateUI();

    public partial class fmMain : Form
    {
        public fmMain()
        {
            InitializeComponent();
            cbxLocationWater.SelectedIndex = 0;
        }

        #region Tiff转Jpg

        private void btnOpDirTiff_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbxDirTiffImg.Text = dialog.SelectedPath;
            }
        }

        private void btnProcessTiff_Click(object sender, EventArgs e)
        {
            this.UpdateResTiff("正在处理...");
            if (ValidateTiffInfo())
            {
                var pathSource = tbxDirTiffImg.Text.Trim();
                var files = Directory.GetFiles(pathSource).Where(c => c.ToLower().EndsWith(".tif")).ToList();
                if (files.Count == 0) { this.UpdateResTiff("未找到任何.tif文件!"); }
                else
                {
                    ThreadPool.QueueUserWorkItem(delegate
                    {
                        var index = 1;
                        files.ForEach(file =>
                        {
                            this.UpdateResTiff(string.Format("共{0}个文件,正在处理第{1}个文件", files.Count, index));
                            var newpath = Path.GetDirectoryName(file) + "\\jpg\\";
                            if (!Directory.Exists(newpath)) Directory.CreateDirectory(newpath);
                            var newfile = newpath + Path.GetFileNameWithoutExtension(file) + ".jpg";
                            File.Copy(file, newfile, true);
                            index++;
                        });
                        this.UpdateResTiff("完成");
                    });
                }
            }            
        }

        private bool ValidateTiffInfo()
        {
            var dirTiffImg =  tbxDirTiffImg.Text.Trim();
            if (dirTiffImg == string.Empty) { this.UpdateResTiff("请输入文件目录!"); return false; }
            return true;
        }

        private void UpdateResTiff(string info)
        {
            labResTiff.Invoke(new UpdateUI(() =>
            {
                labResTiff.Text = info;
            }));
        }

        #endregion

        #region 图片加后缀

        private void btnOpDirSuf_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbxDirSuf.Text = dialog.SelectedPath;
            }
        }

        private void btnProcessSuf_Click(object sender, EventArgs e)
        {
            this.UpdateSufTiff("正在处理...");
            if (ValidateSufInfo())
            {
                var pathSource = tbxDirSuf.Text.Trim();
                var suf = tbxSufInfo.Text.Trim();
                var files = Directory.GetFiles(pathSource).Where(c => c.ToLower().EndsWith(".jpg")).ToList();
                if (files.Count == 0) { this.UpdateSufTiff("未找到任何.jpg文件!"); }
                else
                {
                    ThreadPool.QueueUserWorkItem(delegate
                       {
                           var index = 1;
                           files.ForEach(file =>
                           {
                               this.UpdateSufTiff(string.Format("共{0}个文件,正在处理第{1}个文件", files.Count, index));
                               var newpath = Path.GetDirectoryName(file) + "\\new\\";
                               if (!Directory.Exists(newpath)) Directory.CreateDirectory(newpath);
                               var newfile = newpath + Path.GetFileNameWithoutExtension(file) + suf + ".jpg";
                               File.Copy(file, newfile, true);
                               index++;
                           });
                           this.UpdateSufTiff("完成");
                       });
                }
            }            
        }

        private bool ValidateSufInfo()
        {
            var dirSufImg = tbxDirSuf.Text.Trim();
            if (dirSufImg == string.Empty) { this.UpdateSufTiff("请输入文件目录!"); return false; }
            var sufInfo = tbxSufInfo.Text.Trim();
            if (sufInfo == string.Empty) { this.UpdateSufTiff("请输入后缀!"); return false; }
            return true;
        }

        private void UpdateSufTiff(string info)
        {
            labResSuf.Invoke(new UpdateUI(() =>
            {
                labResSuf.Text = info;
            }));
        }

        #endregion

        #region 生成缩略图
        private void btnOpDirThumb_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbxDirThumbImg.Text = dialog.SelectedPath;
            }

        }

        private void btnProcessThumb_Click(object sender, EventArgs e)
        {
            this.UpdateResThumb("正在处理...");
            if (ValidateThumbInfo())
            {
                var dir = tbxDirThumbImg.Text.Trim();
                var size = tbxSizeThumb.Text.Trim();
                var quality = tbxQualityThumb.Text.Trim();
                var files = Directory.GetFiles(dir).Where(c => c.ToLower().EndsWith(".jpg")).ToList();
                if (files.Count == 0) { this.UpdateResThumb("未找到任何.jpg文件!"); }
                else
                {
                    ThreadPool.QueueUserWorkItem(delegate
                    {
                        var index = 1;
                        files.ForEach(file =>
                        {
                            this.UpdateResThumb(string.Format("共{0}个文件,正在处理第{1}个文件", files.Count, index));
                            ImageHelper.GenerateThumbImg(file, size, int.Parse(quality));
                            index++;
                        });
                        this.UpdateResThumb("完成");
                    });
                }
            }
        }

        private bool ValidateThumbInfo()
        {
            var dirThumbImg = tbxDirThumbImg.Text.Trim();
            if (dirThumbImg == string.Empty) { this.UpdateResThumb("请输入文件目录!"); return false; }
            var sizeThumb = tbxSizeThumb.Text.Trim();
            if (sizeThumb == string.Empty) { this.UpdateResThumb("请输入尺寸!"); return false; }
            var qualityThumb = tbxQualityThumb.Text.Trim();
            if (qualityThumb == string.Empty) { this.UpdateResThumb("请输入质量!"); return false; }
            if (!Regex.IsMatch(qualityThumb, @"[1-9]\d?|100")) { this.UpdateResThumb("质量为:1-100的数据"); return false; }
            return true;
        }

        private void UpdateResThumb(string info)
        {
            labResThumb.Invoke(new UpdateUI(() =>
            {
                labResThumb.Text = info;
            }));
        }

        #endregion

        #region 水印处理

        private void btnOpDirWater_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbxDirWaterImg.Text = dialog.SelectedPath;
            }
        }

        private void btnOpFileWater_Click(object sender, EventArgs e)
        {
            openFileDialog1.Multiselect = false;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tbxImgWater.Text = openFileDialog1.FileName;
            }            
        }

        private void btnProcessWater_Click(object sender, EventArgs e)
        {
            this.UpdateResWater("正在处理...");
            if (ValidateWaterInfo())
            {
                var dir = tbxDirWaterImg.Text.Trim();
                var water = tbxImgWater.Text.Trim();
                var quality = tbxQualityWater.Text.Trim();
                var local = cbxLocationWater.SelectedItem.ToString();
                var posX = int.Parse(tbxPosX.Text.Trim());
                var posY = int.Parse(tbxPosY.Text.Trim());
                var files = Directory.GetFiles(dir).Where(c => c.ToLower().EndsWith(".jpg")).ToList();
                if (files.Count == 0) { this.UpdateResWater("未找到任何.jpg文件!"); }
                else
                {
                    ThreadPool.QueueUserWorkItem(delegate
                    {
                        var index = 1;                        
                        files.ForEach(file =>
                        {
                            this.UpdateResWater(string.Format("共{0}个文件,正在处理第{1}个文件", files.Count, index));
                            var targetPath = Path.GetDirectoryName(file) + "\\water\\";
                            var targetfile = targetPath + Path.GetFileName(file);
                            if (!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath);
                            ImageHelper.GenerateWaterImg(file, water, targetfile, int.Parse(quality), GetMarkPosition(local), posX, posY);
                            //ImageHelper.GenerateThumbImg(file, "40_40", int.Parse(quality));
                            index++;
                        });
                        this.UpdateResWater("完成");
                    });                    
                }
            }
        }

        private bool ValidateWaterInfo()
        {
            var dirWaterImg = tbxDirWaterImg.Text.Trim();
            if (dirWaterImg == string.Empty) { this.UpdateResWater("请输入文件目录!"); return false; }
            var imgWater = tbxImgWater.Text.Trim();
            if (imgWater == string.Empty) { this.UpdateResWater("请输入水印图!"); return false; }
            var qualityWater = tbxQualityWater.Text.Trim();
            if (qualityWater == string.Empty) { this.UpdateResWater("请输入质量!"); return false; }
            if (!Regex.IsMatch(qualityWater, @"[1-9]\d?|100")) { this.UpdateResWater("质量为:1-100的数据"); return false; }
            if (cbxLocationWater.SelectedItem.ToString() == string.Empty) { this.UpdateResWater("请选择位置"); return false; }
            return true;
        }

        private void UpdateResWater(string info)
        {
            labResWater.Invoke(new UpdateUI(() =>
            {
                labResWater.Text = info;
            }));
        }

        private MarkPosition GetMarkPosition(string local)
        {
            switch (local)
            {
                case "左上角": return MarkPosition.Left_Top;
                case "左下角": return MarkPosition.Left_Bottom;
                case "右上角": return MarkPosition.Right_Top;
                case "右下角": return MarkPosition.Right_Bottom;
                default: return MarkPosition.Left_Top;
            }            
        }

        #endregion
    }
}
